package cmpe451.group7.ourcity.beans;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.commons.lang3.StringUtils;

import cmpe451.group7.ourcity.entity.Badge;
import cmpe451.group7.ourcity.entity.Comment;
import cmpe451.group7.ourcity.entity.Place;
import cmpe451.group7.ourcity.entity.User;
import cmpe451.group7.ourcity.service.UserService;
import cmpe451.group7.ourcity.util.Utils;

/**
 * Search bean for using in the search page of the web site.
 */
@ManagedBean
@ViewScoped
public class HomeBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private User homeUser;
	private boolean inUserPage;
	private boolean editUserInfo;
	private boolean editAboutMe;
	private String userInfoText;
	private String aboutMe;
	private List<String> tags;
	private List<Comment> comments;
	private List<Badge> badges;
	private List<Place> favouritePlaces;

	@ManagedProperty(value = "#{loginBean}")
	private LoginBean loginBean;

	@ManagedProperty(value = "#{userService}")
	private UserService userService;

	private long homeUserID;

	public HomeBean() {

		Map<String, String> requestParameterMap = FacesContext
				.getCurrentInstance().getExternalContext()
				.getRequestParameterMap();

		if (requestParameterMap.keySet().size() > 1) {
			// TODO: error correction here
		}

		String userIdString = requestParameterMap.get("userID");

		try {
			homeUserID = StringUtils.isEmpty(userIdString) ? -1 : Long
					.parseLong(userIdString);
		} catch (NumberFormatException e) {
			homeUserID = -1;
		}
	}

	@PostConstruct
	public void initialize() {

		if (homeUserID <= 0)
			try {
				Utils.redirectPage("index.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
			}

		boolean homeUserIsLoginUser = (loginBean.isLoggedIn() && (homeUserID == -1 || homeUserID == loginBean
				.getUserID()));

		homeUser = userService.fullyLoadUser(homeUserID);
		if (homeUser == null) {
			try {
				Utils.redirectPage("index.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		if (homeUserIsLoginUser) {
			inUserPage = true;
			setUserInfoText(homeUser.getUserInfo());
			setAboutMe(homeUser.getAboutMe());
		}

		setTags(userService.getCommentTagsofUser(homeUser));
		setComments(userService.getCommentsofUser(homeUser));
		setFavouritePlaces(new ArrayList<Place>(homeUser.getLikedPlace()));
		setBadges(new ArrayList<Badge>(homeUser.getBadges()));
	}

	public User getHomeUser() {
		return homeUser;
	}

	public void setHomeUser(User homeUser) {
		this.homeUser = homeUser;
	}

	public LoginBean getLoginBean() {
		return loginBean;
	}

	public void setLoginBean(LoginBean loginBean) {
		this.loginBean = loginBean;
	}

	public boolean isInUserPage() {
		return inUserPage;
	}

	public void setInUserPage(boolean inUserPage) {
		this.inUserPage = inUserPage;
	}

	public boolean isEditUserInfo() {
		return editUserInfo;
	}

	public void setEditUserInfo(boolean editUserInfo) {
		this.editUserInfo = editUserInfo;
	}

	public void editUserInfoFonk() {
		editUserInfo = true;
		inUserPage = false;
	}

	public void editAboutMeFonk() {
		editAboutMe = true;
		inUserPage = false;
	}

	public String setUserInfoFonk() {
		userService.updateUserInfo(userInfoText, homeUser);
		inUserPage = true;
		editUserInfo = false;
		return "home.xhtml?userID=" + homeUserID + "&faces-redirect=true";
	}

	public String setAboutMeFonk() {
		userService.updateAboutMe(aboutMe, homeUser);
		inUserPage = true;
		editAboutMe = false;
		return "home.xhtml?userID=" + homeUserID + "&faces-redirect=true";
	}

	public boolean isEditAboutMe() {
		return editAboutMe;
	}

	public void setEditAboutMe(boolean editAboutMe) {
		this.editAboutMe = editAboutMe;
	}

	public String getAboutMe() {
		return aboutMe;
	}

	public void setAboutMe(String aboutMe) {
		this.aboutMe = aboutMe;
	}

	public String getUserInfoText() {
		return userInfoText;
	}

	public void setUserInfoText(String userInfoText) {
		this.userInfoText = userInfoText;
	}

	public List<String> getTags() {
		return tags;
	}

	public void setTags(List<String> tags) {
		this.tags = tags;
	}

	public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}

	public List<Place> getFavouritePlaces() {
		return favouritePlaces;
	}

	public void setFavouritePlaces(List<Place> favouritePlaces) {
		this.favouritePlaces = favouritePlaces;
	}

	public List<Badge> getBadges() {
		return badges;
	}

	public void setBadges(List<Badge> badges) {
		this.badges = badges;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
